import re

# findall:匹配字符串中所有的符合正则的内容
# fl_list = re.findall(r"\d+", "my QQ is 1316087063, my phone is 10086")  # r（转义字符）没什么作用，就是为了丢掉"\d+"的下波浪
# print(fl_list)

# finditer:匹配字符串中所有的符合正则的内容,但返回的是迭代器
# fr_list = re.finditer(r"\d+", "my QQ is 1316087063, my phone is 10086")  # r（转义字符）没什么作用，就是为了丢掉"\d+"的下波浪
# print(fr_list)
# for i in fr_list:
#     print(i.group())  # group()方法返回的是匹配的内容

# search:匹配字符串中第一个符合正则的内容
# s_list = re.search(r"\d+", "my QQ is 1316087063, my phone is 10086")
# print(s_list)
# print(s_list.group())  # group()方法返回的是匹配的内容

# match:匹配字符串开头符合正则的内容
# m_list = re.match(r"\d+", "my QQ is 1316087063, my phone is 10086")
# print(m_list)  # 匹配失败返回None

# 预加载正则表达式
# pattern = re.compile(r"\d+") 将正则表达式模式编译成一个正则表达式对象,正则表达式的字符串模式换成内部的、更高效的数据结构或字节码，以便匹配引擎能够快速地执行匹配操作。
# re_fl_list = pattern.findall("my QQ is 1316087063, my phone is 10086")

S = """
<div class='cat'><span id='1'>哈基米</span></div>
<div class='STS'><span id='2'>姚明辉</span></div>
<div class='genius'><span id='3'>大聪明</span></div>
<div class='talent'><span id='4'>天赋</span></div>
<div class='bullshit'><span id='5'>胡说八道</span></div>
"""
pattern = re.compile(r"<div class='(?P<name1>.*?)'><span id='(.*?)'>(.*?)</span></div>", re.S)  # re.S表示匹配换行符
re_fl_list = pattern.finditer(S)
for i in re_fl_list:
    print(i.group("name1"))
